package dmp.llapi.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hdfs.DFSClient.Conf;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

public class FlexApiMapper extends Mapper<LongWritable, Text, Text, NullWritable> {

	private Log log = LogFactory.getLog(FlexApiMapper.class);

	private static Map<String, String> urlInfoMap = new HashMap<String, String>();

	@Override
	protected void setup(Mapper<LongWritable, Text, Text, NullWritable>.Context context) throws IOException, InterruptedException {
		Configuration conf = context.getConfiguration();
		// 用map装载配置信息
		for (String urlInfo : conf.getStrings("domains")) {
			String[] array = urlInfo.split("\\|");
			if (array.length >= 2) {
				String name = array[0];
				urlInfoMap.put(name, urlInfo);
			}
		}
	}

	@Override
	protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
		 log.info("GGGG---"+urlInfoMap.get("autohome"));
		 log.info("GGGG---"+urlInfoMap.get("yiche"));
		 log.info("GGGG---"+urlInfoMap.get("huimaiche"));
		String line = value.toString();
		String rr = URLUtils.getValueByParam(line, urlInfoMap);
		log.info("ggggg---" + rr);
		
		context.write(new Text(rr), NullWritable.get());
	}

//	public static void main(String[] args) {
//		
//		List<String> domainList = UrlParseList.elements();
//		String[] domains = new String[domainList.size()];
//		
//		domainList.toArray(domains);
//		
//		
//		for (String domain :domains ) {
//			String[] array = domain.split("\\|");
//			if (array.length >= 2) {
//				String name = array[0];
//				map.put(name, domain);
//			}
//		}
//		
//		System.out.println(map.get("yiche"));
//		
//		String url = "http://api.app.yiche.com/webapi/api.ashx?method=bit.seriessummary&serialid=3721&cityid=1201&sign=f6f54821dfb46fd942d09320ea8aec08 ";
//		String rr = URLUtils.getValueByParam(url, map);
//		
//		System.out.println(rr);
//	}

}
